﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Threading;


namespace WindowsApplication1
{

  public partial class Editor : Form
  {
    public const string STATFilter = "Stat Files (*.stat)|*.stat";
    public const string JPGFilter = "JPG images (*.jpg)|*.jpg";
    public const string BMPFilter = "BMP images (*.bmp)|*.bmp";
    public const string GIFFilter = "GIF images (*.gif)|*.gif";
    public const string ImageFilter = JPGFilter + "|" + BMPFilter + "|" + GIFFilter;

    public const string BothFilter = STATFilter + "|" + ImageFilter;
    public const string AllFilter = "All Files (*.*)|*.*";
    public const string AllFilters = AllFilter + "|" + BothFilter;

    public static Editor instance;

    private int openedCount = 0;
    private int OpenedCount
    {
      get { return openedCount; }
      set
      {
        openedCount = value;
      }
    }

    public Editor()
    {
      InitializeComponent();
      MdiChildActivate += new EventHandler(MDIParent1_MdiChildActivate);
      instance = this;
      OpenedCount = 0;
    }

    void MDIParent1_MdiChildActivate(object sender, EventArgs e)
    {
      bool active = (GetActiveChild() != null);
      saveToolStripButton.Enabled = active;
      saveToolStripMenuItem.Enabled = active;
      saveAsToolStripMenuItem.Enabled = active;
      closeToolStripMenuItem.Enabled = active;
    }

      private void OpenFile(object sender, EventArgs e)
      {
          OpenFileDialog openFileDialog = new OpenFileDialog();
          openFileDialog.Filter = AllFilters;

          if (openFileDialog.ShowDialog(this) == DialogResult.OK)
          {
              string FileName = openFileDialog.FileName;
              if (FileName.EndsWith(".stat", StringComparison.CurrentCultureIgnoreCase))
              {
                  Element[] els = readkNNElements(FileName);
                  Form childForm = new KNNForm(els);
                  childForm.MdiParent = this;
                  childForm.Show();
              }

              StringComparison sc = StringComparison.CurrentCultureIgnoreCase;
              if (FileName.EndsWith(".bmp", sc) || FileName.EndsWith(".jpg", sc) || FileName.EndsWith(".gif", sc))
              {
                  Form childForm = new FuzzyISODataForm(FileName);
                  childForm.MdiParent = this;
                  childForm.Show();
              }
          }
      }

      public static Element[] readkNNElements(string fname)
      {
          TextReader reader = new StreamReader(fname);
          try
          {
              string line;
              int elementCount = 0;
              int featuresCount = 0;

              if ((line = reader.ReadLine()) != null)
                  elementCount = Int32.Parse(line);
              if ((line = reader.ReadLine()) != null)
                  featuresCount = Int32.Parse(line);

              Element[] elements = new Element[elementCount];

              for (int j = 0; j < elementCount; j++)
              {
                  line = reader.ReadLine();
                  string[] numbers = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                  Element el = new Element(featuresCount);
                  int i;
                  for (i = 0; i < numbers.Length - 1; i++)
                      el.FeatureValues[i] = Double.Parse(numbers[i]);
                  el.Class = Int32.Parse(numbers[i]);

                  elements[j] = el;
              }
              return elements;
          }
          catch (Exception e)
          {
              Console.WriteLine(e);
          }
          finally
          {
              reader.Close();
          }
          return null;
      }

    private void saveToolStripMenuItem_Click(object sender, EventArgs e)
    {
    }

    private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
    {
    }

    private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
    {
       this.Close();
    }

    private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
    {
      toolStrip.Visible = toolBarToolStripMenuItem.Checked;
    }

    private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
    {
      statusStrip.Visible = statusBarToolStripMenuItem.Checked;
    }

    private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
    {
      LayoutMdi(MdiLayout.Cascade);
    }

    private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
    {
      LayoutMdi(MdiLayout.TileVertical);
    }

    private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
    {
      LayoutMdi(MdiLayout.TileHorizontal);
    }

    private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
    {
      LayoutMdi(MdiLayout.ArrangeIcons);
    }

    private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
    {
      foreach (Form childForm in MdiChildren)
      {
        childForm.Close();
      }
    }

    private Form GetActiveChild()
    {
      return ActiveMdiChild;
    }


    private void closeToolStripMenuItem_Click(object sender, EventArgs e)
    {
      Form form = GetActiveChild();
      form.Close();
    }

    private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
    {
      string about = "Program zaliczeniowy ćw. nr 3 laboratorium Wizji Komuterowej.\n";
      about += "Prowadzący laboratorium: mgr inż. Marcin Michalak\n";
      about += "Sekcja nr 14, skład sekcji:\n";
      about += "Bartłomiej Orłowski,\n";
      about += "\nInformatyka\n";
      about += "Wydział Automatyki, Elektroniki i Informatyki\n";
      about += "Politechnika Śląska, Gliwice\n";
      string title = "About";
      MessageBox.Show(about, title, MessageBoxButtons.OK, 
        MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
    }

  }
}
